//
//  AddNewProjectController.m
//  YellowBridge
//
//  Created by 王右 on 2018/12/3.
//  Copyright © 2018 王右. All rights reserved.
//

#import "AddNewProjectController.h"
#import "AddProjectInfoEditCell.h"
#import "ImagesAndContentTableViewCell.h"
#import "ImagesWithOutContentTableViewCell.h"
#import "ProjectDetailModel.h"
#import "PGDatePickManager.h"
#import "AppDelegate.h"

@interface AddNewProjectController ()

@property (strong, nonatomic) IBOutlet UITableViewCell *firstCell;
@property (weak, nonatomic) IBOutlet UITableView *baseTableView;
@property (weak, nonatomic) IBOutlet UITableView *infoTableView;

@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSMutableArray *detailArr;
@property (nonatomic, strong) NSMutableArray *uploadTextStringArr;

@property (nonatomic, strong) ImagesAndContentTableViewCell *technologicalPlanCell;
@property (nonatomic, strong) ImagesAndContentTableViewCell *otherFilesCell;
@property (nonatomic, strong) ImagesAndContentTableViewCell *notesCell;
@property (nonatomic, strong) ImagesWithOutContentTableViewCell *baoJianShouXuCell;

@property (nonatomic, strong) PGDatePicker *startDayPicker;

@property (nonatomic, strong) PGDatePicker *endDayPicker;

@property (strong, nonatomic) IBOutlet UITableViewCell *bottomCell;


@end

@implementation AddNewProjectController

- (void)viewDidLoad {
    [super viewDidLoad];

    if (!_isUpdateProjectInfo) {
        self.title = @"新增项目";
    }else{
        self.title = @"更新项目";
    }
    _titleArr = @[@"项目名称:",@"项目类别:",@"项目性质:",@"项目规模:",@"项目状态:",@"项目位置:",@"建设单位:",@"代建单位:",@"施工单位:",@"监理单位:",@"设计单位:",@"勘察单位:",@"建设年限:",@"计划总投资:",@"道路等级:",@"棚改融资项目情况:",@"开工时间:",@"交付时间:"].mutableCopy;
    if (!_isUpdateProjectInfo) {
        _detailArr = @[@"请输入项目名称",@"请选择",@"请选择",@"请选择",@"请选择",@"请输入项目位置",@"请填写建设单位",@"请填写代建单位",@"请填写施工单位",@"请输入监理单位",@"请输入设计单位",@"请输入勘察单位",@"请填写建设年限",@"请输入计划总投资",@"请填写道路等级",@"请输入棚改融资项目情况",@"请选择",@"请选择"].mutableCopy;
        
        _uploadTextStringArr = @[@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@""].mutableCopy;
    }else{
        [self configInfoModelWithModel:_projectDetailModel];
    }
    
    _technologicalPlanCell = (ImagesAndContentTableViewCell *)[_baseTableView cellWithCellNibName:@"ImagesAndContentTableViewCell"];
    
    _otherFilesCell = (ImagesAndContentTableViewCell *)[_baseTableView cellWithCellNibName:@"ImagesAndContentTableViewCell"];

    _notesCell = (ImagesAndContentTableViewCell *)[_baseTableView cellWithCellNibName:@"ImagesAndContentTableViewCell"];

    _baoJianShouXuCell = (ImagesWithOutContentTableViewCell *)[_baseTableView cellWithCellNibName:@"ImagesWithOutContentTableViewCell"];
}

- (void)configInfoModelWithModel:(ProjectDetailModel *)model{
    _detailArr = @[model.projectname,model.pmtype, model.pmxz,model.pmgm,model.pmstatus,model.pmposition,model.pmjsdw,model.pmdjdw,model.pmsgdw,model.pmjldw,model.pmsjdw,model.pmkcdw,model.pmjslx,model.pmjhztz,model.pmroadgrade,model.pmpgrzqk,[model.pmstartday retureTimeStringWithFormate:@"yyyy-MM-dd"],[model.pmdeliverday retureTimeStringWithFormate:@"yyyy-MM-dd"]].mutableCopy;
    
    _uploadTextStringArr = @[model.projectname,model.pmtype, model.pmxz,model.pmgm,model.pmstatus,model.pmposition,model.pmjsdw,model.pmdjdw,model.pmsgdw,model.pmjldw,model.pmsjdw,model.pmkcdw,model.pmjslx,model.pmjhztz,model.pmroadgrade,model.pmpgrzqk,model.pmstartday,model.pmdeliverday].mutableCopy;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == _baseTableView) {
        return 6;
    }else if (tableView == _infoTableView){
        return _titleArr.count;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (tableView == _baseTableView) {
        if (indexPath.row==0) {
            NSInteger count = _titleArr.count;
            if ([_detailArr[1] integerValue]!=1) {
                count-=1;
            }
            if ([_detailArr[1] integerValue]!=2) {
                count-=1;
            }
            return count * 50;
        }else if (indexPath.row == 1){
            return [_technologicalPlanCell returnCellHeight];
        }else  if (indexPath.row == 2){
            return [_baoJianShouXuCell returnCellHeight];
        }else if (indexPath.row == 3){
            return [_otherFilesCell returnCellHeight];
        }else if (indexPath.row == 4){
            return [_notesCell returnCellHeight];
        }else{
            return 60;
        }
    }else if (tableView == _infoTableView){
        if (indexPath.row == 14) {
            if ([_detailArr[1] integerValue] == 1) {
                return 50;
            }else{
                return 0;
            }
        }
        if (indexPath.row == 15) {
            if ([_detailArr[1] integerValue] == 2) {
                return 50;
            }else{
                return 0;
            }
        }
        return 50;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    //新建项目
    if (tableView == _baseTableView) {
        if (indexPath.row == 0) {
            return _firstCell;
        }else if (indexPath.row == 1){
            _technologicalPlanCell.superTableView = tableView;
            _technologicalPlanCell.nameLabel.text = @"经济技术指标";
            _technologicalPlanCell.canEdit = YES;
            if (!_technologicalPlanCell.isContentReload) {
                _technologicalPlanCell.imageArr = _projectDetailModel.zbImageArr;
                _technologicalPlanCell.textViewText = _projectDetailModel.jjzbdesc;
            }
            return _technologicalPlanCell;
        }else if (indexPath.row == 2){
            _baoJianShouXuCell.superTableView = tableView;
            _baoJianShouXuCell.nameLabel.text = @"报建手续";
            if (!_baoJianShouXuCell.isContentReload) {
                _baoJianShouXuCell.fagaiImageArr = _projectDetailModel.fgImageArr;
                _baoJianShouXuCell.guotuImageArr = _projectDetailModel.gtImageArr;
                _baoJianShouXuCell.guihuaImageArr = _projectDetailModel.ghImageArr;
                _baoJianShouXuCell.huanbaoImageArr = _projectDetailModel.hbImageArr;
                _baoJianShouXuCell.zhujianImageArr = _projectDetailModel.zjImageArr;
                
                _baoJianShouXuCell.fagaiTextViewText = _projectDetailModel.fgdesc;
                _baoJianShouXuCell.guoTuTextViewText = _projectDetailModel.gtdesc;
                _baoJianShouXuCell.zhuJianTextViewText = _projectDetailModel.zjdesc;
                _baoJianShouXuCell.huanBaoTextViewText = _projectDetailModel.hbdesc;
                _baoJianShouXuCell.guiHuaTextViewText = _projectDetailModel.ghdesc;
                
//                _baoJianShouXuCell.textViewText = _projectDetailModel.bjConent;
                _baoJianShouXuCell.type = BaoJianShouXuCellTypeFaGai;
            }
            _baoJianShouXuCell.canEdit = YES;
            return _baoJianShouXuCell;
        }else if (indexPath.row == 3){
            _otherFilesCell.superTableView = tableView;
            _otherFilesCell.nameLabel.text = @"其他附件";
            if (!_otherFilesCell.isContentReload) {
                _otherFilesCell.textViewText = _projectDetailModel.qtdesc;
                _otherFilesCell.imageArr = _projectDetailModel.qtImageArr;
            }
            _otherFilesCell.canEdit = YES;
            return _otherFilesCell;
        }else if (indexPath.row == 4){
            _notesCell.superTableView = tableView;
            _notesCell.nameLabel.text = @"备注说明";
            if (!_notesCell.isContentReload) {
                _notesCell.textViewText = _projectDetailModel.pmdesc;
                _notesCell.imageArr = _projectDetailModel.bzImageArr;
            }
            _notesCell.canEdit = YES;
            return _notesCell;
        }else{
            return _bottomCell;
        }
    }else if (tableView == _infoTableView){
        AddProjectInfoEditCell *cell = (AddProjectInfoEditCell *)[tableView cellWithCellNibName:@"AddProjectInfoEditCell"];
        cell.nameLabel.text = _titleArr[indexPath.row];
        if (!_isUpdateProjectInfo) {
            cell.detailTextField.placeholder = _detailArr[indexPath.row];
        }else{
            cell.detailTextField.text = _detailArr[indexPath.row];
        }
        cell.block = ^(NSString * _Nonnull textString) {
            if (textString) {
                [self->_uploadTextStringArr replaceObjectAtIndex:indexPath.row withObject:textString];
            }
        };
        if (indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3 || indexPath.row==4) {
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            cell.detailTextField.enabled = NO;
            if (indexPath.row==1 && ![_detailArr[indexPath.row] isEqualToString:@"请选择"]) {
                switch ([_detailArr[indexPath.row] integerValue]) {
                    case 1:
                        cell.detailTextField.text = @"市政道路";
                        break;
                    case 2:
                        cell.detailTextField.text = @"居住建筑";
                        break;
                    case 3:
                        cell.detailTextField.text = @"公共配套";
                        break;
                    case 4:
                        cell.detailTextField.text = @"其他";
                        break;
                    default:
                        cell.detailTextField.text = @"请选择";
                        break;
                }
            }
            if (indexPath.row==2 && ![_detailArr[indexPath.row] isEqualToString:@"请选择"]) {
                switch ([_detailArr[indexPath.row] integerValue]) {
                    case 1:
                        cell.detailTextField.text = @"新建项目";
                        break;
                    case 2:
                        cell.detailTextField.text = @"扩建项目";
                        break;
                    case 3:
                        cell.detailTextField.text = @"改建项目";
                        break;
                    default:
                        cell.detailTextField.text = @"请选择";
                        break;
                }
            }
            if (indexPath.row==3 && ![_detailArr[indexPath.row] isEqualToString:@"请选择"]) {
                if ([_detailArr[indexPath.row] isEqualToString:@"A"]) {
                    cell.detailTextField.text = @"大型项目";
                }else if ([_detailArr[indexPath.row] isEqualToString:@"B"]){
                    cell.detailTextField.text = @"中型项目";
                }else{
                    cell.detailTextField.text = @"小型项目";
                }
            }
            if (indexPath.row==4 && ![_detailArr[indexPath.row] isEqualToString:@"请选择"]) {
                if ([_detailArr[indexPath.row] integerValue] == 1) {
                    cell.detailTextField.text = @"未竣工";
                }else{
                    cell.detailTextField.text = @"已竣工";
                }
            }
            
        }else{
            if (indexPath.row == 16 || indexPath.row == 17) {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.detailTextField.enabled = NO;
                if (![_detailArr[indexPath.row] isEqualToString:@"请选择"]) {
                    cell.detailTextField.text = _detailArr[indexPath.row];
                }
            }else{
                cell.accessoryType = UITableViewCellAccessoryNone;
                cell.detailTextField.enabled = YES;
                if (indexPath.row == 12 || indexPath.row == 13) {
                    cell.detailTextField.keyboardType = UIKeyboardTypeNumberPad;
                }else{
                    cell.detailTextField.keyboardType = UIKeyboardTypeDefault;
                }
            }
        }
        if (indexPath.row==14) {
            if (!_isUpdateProjectInfo) {
                cell.detailTextField.text = nil;
                cell.detailTextField.placeholder = _detailArr[indexPath.row];
            }else{
                cell.detailTextField.text = _detailArr[indexPath.row];
            }
        }else if(indexPath.row == 15){
            if (!_isUpdateProjectInfo) {
                cell.detailTextField.text = nil;
                cell.detailTextField.placeholder = _detailArr[indexPath.row];
            }else{
                cell.detailTextField.text = _detailArr[indexPath.row];
            }
        }
        return cell;
    }
    return [UITableViewCell new];
}


- (IBAction)cancelButtonAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)submitButtonAction:(id)sender {
    if (!_isUpdateProjectInfo) {
        [self addProjectAction];
    }else{
        [self updateProjectAction];
    }
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.view endEditing:YES];
    if (tableView == _infoTableView) {
        if (indexPath.row==1) {
            __weak typeof(self) weakSelf = self;
            //        项目类别（1-市政道路、2-居住建筑、3-公共配套，4-其他）
            LCActionSheet *sheet = [LCActionSheet sheetWithTitle:@"请选择项目类别" buttonTitles:@[@"市政道路",@"居住建筑",@"公共配套",@"其他"] redButtonIndex:-1 buttonClickedCallback: ^(NSInteger index) {
                if (index!=-1) {
                    [weakSelf.detailArr replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%@",@(index+1)]];
                    [weakSelf.uploadTextStringArr replaceObjectAtIndex:1 withObject:[NSString stringWithFormat:@"%@",@(index+1)]];
                    if (index==0) {
                        [self->_uploadTextStringArr replaceObjectAtIndex:15 withObject:@""];
                        [self->_detailArr replaceObjectAtIndex:14 withObject:@"请填写道路等级"];
                    }else if (index==1) {
                        [self->_uploadTextStringArr replaceObjectAtIndex:14 withObject:@""];
                        [self->_detailArr replaceObjectAtIndex:15 withObject:@"请填写棚改项目信息"];
                    }else{
                        [self->_uploadTextStringArr replaceObjectAtIndex:14 withObject:@""];
                        [self->_uploadTextStringArr replaceObjectAtIndex:15 withObject:@""];
                        [self->_detailArr replaceObjectAtIndex:14 withObject:@"请填写道路等级"];
                        [self->_detailArr replaceObjectAtIndex:15 withObject:@"请填写棚改项目信息"];
                    }
                    [weakSelf.infoTableView reloadRowsAtIndexPaths:@[indexPath,[NSIndexPath indexPathForRow:14 inSection:0],[NSIndexPath indexPathForRow:15 inSection:0]] withRowAnimation:(UITableViewRowAnimationNone)];
                    [weakSelf.baseTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:(UITableViewRowAnimationNone)];
                }
            }];
            [sheet show];
        }else if (indexPath.row==2){
            __weak typeof(self) weakSelf = self;
            //        项目性质（1-新建项目、2-扩建项目、3-改建项目)
            LCActionSheet *sheet = [LCActionSheet sheetWithTitle:@"请选择项目性质" buttonTitles:@[@"新建项目",@"扩建项目",@"改建项目"] redButtonIndex:-1 buttonClickedCallback: ^(NSInteger index) {
                if (index!=-1) {
                    [weakSelf.detailArr replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"%@",@(index+1)]];
                    [weakSelf.uploadTextStringArr replaceObjectAtIndex:2 withObject:[NSString stringWithFormat:@"%@",@(index+1)]];
                    [weakSelf.infoTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationNone)];
                }
            }];
            [sheet show];
        }else if (indexPath.row==3){
            __weak typeof(self) weakSelf = self;
            //      项目规模（A-大型、B-中型、C-小型）
            LCActionSheet *sheet = [LCActionSheet sheetWithTitle:@"请选择项目规模" buttonTitles:@[@"大型项目",@"中型项目",@"小型项目"] redButtonIndex:-1 buttonClickedCallback: ^(NSInteger index) {
                if (index==0) {

                    [weakSelf.detailArr replaceObjectAtIndex:3 withObject:@"A"];
                    [weakSelf.uploadTextStringArr replaceObjectAtIndex:3 withObject:@"A"];
                }else if (index==1){
                    [weakSelf.detailArr replaceObjectAtIndex:3 withObject:@"B"];
                    [weakSelf.uploadTextStringArr replaceObjectAtIndex:3 withObject:@"B"];
                }else if (index==2){
                    [weakSelf.detailArr replaceObjectAtIndex:3 withObject:@"C"];
                    [weakSelf.uploadTextStringArr replaceObjectAtIndex:3 withObject:@"C"];
                }
                [weakSelf.infoTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationNone)];
            }];
            [sheet show];
        }else if (indexPath.row == 4){
            __weak typeof(self) weakSelf = self;
            //      项目状态（1-未竣工、2-已竣工）
            LCActionSheet *sheet = [LCActionSheet sheetWithTitle:@"请选择项目状态" buttonTitles:@[@"未竣工",@"已竣工"] redButtonIndex:-1 buttonClickedCallback: ^(NSInteger index) {
                if (index!=-1) {
                    [weakSelf.detailArr replaceObjectAtIndex:4 withObject:[NSString stringWithFormat:@"%@",@(index+1)]];
                    [weakSelf.uploadTextStringArr replaceObjectAtIndex:4 withObject:[NSString stringWithFormat:@"%@",@(index+1)]];
                    [weakSelf.infoTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationNone)];
                }
            }];
            [sheet show];
        }if (indexPath.row == 16) {
            //开工时间
            __weak typeof(self) weakSelf = self;
            PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
            PGDatePicker *datePicker = datePickManager.datePicker;
            datePicker.datePickerMode = PGDatePickerModeDate;
            datePicker.selectedDate = ^(NSDateComponents *dateComponents) {
                NSLog(@"%ld-%ld-%ld",dateComponents.year,dateComponents.month,dateComponents.day);
                NSCalendar * calendar = [NSCalendar currentCalendar];
                NSDate *date = [calendar dateFromComponents:dateComponents];
                NSTimeInterval time = [date timeIntervalSince1970];
                [weakSelf.detailArr replaceObjectAtIndex:16 withObject:[NSString stringWithFormat:@"%ld-%ld-%ld",dateComponents.year,dateComponents.month,dateComponents.day]];
                [self->_uploadTextStringArr replaceObjectAtIndex:16 withObject:[NSString stringWithFormat:@"%d",(int)time]];
                [weakSelf.infoTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationNone)];
            };
            [self presentViewController:datePickManager animated:false completion:nil];
        }else if (indexPath.row == 17){
            __weak typeof(self) weakSelf = self;
            //竣工时间
            PGDatePickManager *datePickManager = [[PGDatePickManager alloc]init];
            PGDatePicker *datePicker = datePickManager.datePicker;
            datePicker.datePickerMode = PGDatePickerModeDate;
            datePicker.selectedDate = ^(NSDateComponents *dateComponents) {
                NSLog(@"%ld-%ld-%ld",dateComponents.year,dateComponents.month,dateComponents.day);
                NSCalendar * calendar = [NSCalendar currentCalendar];
                NSDate *date = [calendar dateFromComponents:dateComponents];
                NSTimeInterval time = [date timeIntervalSince1970];
                [weakSelf.detailArr replaceObjectAtIndex:17 withObject:[NSString stringWithFormat:@"%ld-%ld-%ld",dateComponents.year,dateComponents.month,dateComponents.day]];
                [self->_uploadTextStringArr replaceObjectAtIndex:17 withObject:[NSString stringWithFormat:@"%d",(int)time]];
                [weakSelf.infoTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationNone)];
            };
            [self presentViewController:datePickManager animated:false completion:nil];
        }
    }
}
//新增项目
- (void)addProjectAction{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSMutableDictionary *pmModel = [NSMutableDictionary dictionary];
    [dic setValue:User_Account forKey:@"account"];
    [dic setValue:User_SignToken forKey:@"token"];
    NSArray *keyArr = @[@"projectname",@"pmtype",@"pmxz",@"pmgm",@"pmstatus",@"pmposition",@"pmjsdw",@"pmdjdw",@"pmsgdw",@"pmjldw",@"pmsjdw",@"pmkcdw",@"pmjslx",@"pmjhztz",@"pmroadgrade",@"pmpgrzqk",@"pmstartday",@"pmdeliverday"];
    
   
    
    if ([_uploadTextStringArr[0] length]==0) {
        [self.view makeToast:@"请填写项目名称!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[0] forKey:keyArr[0]];
    
    if ([_uploadTextStringArr[1] length] ==0){
        [self.view makeToast:@"请选择项目类型!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[1] forKey:keyArr[1]];

    if ([_uploadTextStringArr[2] length] ==0){
        [self.view makeToast:@"请选择项目性质!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[2] forKey:keyArr[2]];
    
    if ([_uploadTextStringArr[3] length] ==0){
        [self.view makeToast:@"请选择项目规模!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[3] forKey:keyArr[3]];
    
    if ([_uploadTextStringArr[4] length] ==0){
        [self.view makeToast:@"请选择项目状态!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[4] forKey:keyArr[4]];
    
    if ([_uploadTextStringArr[5] length] ==0){
        [self.view makeToast:@"请填写项目位置!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[5] forKey:keyArr[5]];
    
    if ([_uploadTextStringArr[6] length] ==0){
        [self.view makeToast:@"请填写项目位置!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[6] forKey:keyArr[6]];
    
    if ([_uploadTextStringArr[7] length] ==0){
        [self.view makeToast:@"请填写建设单位!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[7] forKey:keyArr[7]];
    
    if ([_uploadTextStringArr[8] length] ==0){
        [self.view makeToast:@"请填写代建单位!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[8] forKey:keyArr[8]];
    
    if ([_uploadTextStringArr[9] length] ==0){
        [self.view makeToast:@"请填写施工单位!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[9] forKey:keyArr[9]];
    
    if ([_uploadTextStringArr[10] length] ==0){
        [self.view makeToast:@"请填写监理单位!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[10] forKey:keyArr[10]];
    
    if ([_uploadTextStringArr[11] length] ==0){
        [self.view makeToast:@"请填写设计单位!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[11] forKey:keyArr[11]];
    
    if ([_uploadTextStringArr[12] length] ==0){
        [self.view makeToast:@"请填写勘察单位!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[12] forKey:keyArr[12]];
    
    if ([_uploadTextStringArr[13] length] ==0){
        [self.view makeToast:@"请填写计划总投资!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[13] forKey:keyArr[13]];
    
    if ([_uploadTextStringArr[14] length] == 0 && [_uploadTextStringArr[1] integerValue] == 1){
        [self.view makeToast:@"请填写道路级别!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[14] forKey:keyArr[14]];
    
    if ([_uploadTextStringArr[5] length] ==0 && [_uploadTextStringArr[1] integerValue] == 2){
        [self.view makeToast:@"请填写融资情况!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[15] forKey:keyArr[15]];
    
    if ([_uploadTextStringArr[16] length] ==0){
        [self.view makeToast:@"请选择项目开工时间!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[16] forKey:keyArr[16]];
    
    if ([_uploadTextStringArr[17] length] ==0){
        [self.view makeToast:@"请项目交付时间!"];
        return;
    }
    [pmModel setValue:_uploadTextStringArr[17] forKey:keyArr[17]];

    if ([_uploadTextStringArr[16] integerValue] > [_uploadTextStringArr[17] integerValue]) {
        [self.view makeToast:@"项目交付时间不得早于项目开工时间!"];
        return; 
    }
    
    [pmModel setValue:_otherFilesCell.textViewText.length==0?@"":_otherFilesCell.textViewText forKey:@"qtdesc"];
    
    [pmModel setValue:_technologicalPlanCell.textViewText.length==0?@"":_technologicalPlanCell.textViewText forKey:@"jjzbdesc"];
    [pmModel setValue:_baoJianShouXuCell.fagaiTextViewText.length==0?@"":_baoJianShouXuCell.fagaiTextViewText forKey:@"fgdesc"];
    [pmModel setValue:_baoJianShouXuCell.guoTuTextViewText.length==0?@"":_baoJianShouXuCell.guoTuTextViewText forKey:@"gtdesc"];
    
    [pmModel setValue:_baoJianShouXuCell.guiHuaTextViewText.length==0?@"":_baoJianShouXuCell.guiHuaTextViewText forKey:@"ghdesc"];
    [pmModel setValue:_baoJianShouXuCell.huanBaoTextViewText.length==0?@"":_baoJianShouXuCell.huanBaoTextViewText forKey:@"hbdesc"];
    [pmModel setValue:_baoJianShouXuCell.zhuJianTextViewText.length==0?@"":_baoJianShouXuCell.zhuJianTextViewText forKey:@"zjdesc"];
    
    [pmModel setValue:_notesCell.textViewText.length==0?@"":_notesCell.textViewText forKey:@"pmdesc"];
    
    [dic setValue:pmModel forKey:@"pmmodel"];

    
    if (_technologicalPlanCell.imageArr.count!=0) {
        
        //添加经济技术指标参数
        NSMutableArray *tecArr = [NSMutableArray array];
        for (NSString *imageString in _technologicalPlanCell.imageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"zb",@"attachdesc":_technologicalPlanCell.textViewText.length==0?@"":_technologicalPlanCell.textViewText};
            [tecArr addObject:dic1];
        }
        [dic setValue:tecArr forKey:@"jjzb"];
    }else{
        [dic setValue:[NSArray new] forKey:@"jjzb"];
    }
    
    if (_baoJianShouXuCell.fagaiImageArr.count!=0) {

        //添加发改参数
        NSMutableArray *fagai = [NSMutableArray array];
        for (NSString *imageString in _baoJianShouXuCell.fagaiImageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"fg",@"attachdesc":_baoJianShouXuCell.fagaiTextViewText.length!=0?_baoJianShouXuCell.fagaiTextViewText:@""};
            [fagai addObject:dic1];
        }
        [dic setValue:fagai forKey:@"fg"];
    }else{
        [dic setValue:[NSArray new] forKey:@"fg"];
    }
    

    if (_baoJianShouXuCell.guotuImageArr.count!=0) {
        //添加国土参数
        NSMutableArray *guotu = [NSMutableArray array];
        for (NSString *imageString in _baoJianShouXuCell.guotuImageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"gt",@"attachdesc":_baoJianShouXuCell.guoTuTextViewText.length!=0?_baoJianShouXuCell.guoTuTextViewText:@""};
            [guotu addObject:dic1];
        }
        [dic setValue:guotu forKey:@"gt"];
    }else{
        [dic setValue:[NSArray new] forKey:@"gt"];
    }


    if (_baoJianShouXuCell.guihuaImageArr.count!=0) {
        //添加规划参数
        NSMutableArray *gh = [NSMutableArray array];
        for (NSString *imageString in _baoJianShouXuCell.guihuaImageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"gh",@"attachdesc":_baoJianShouXuCell.guiHuaTextViewText.length!=0?_baoJianShouXuCell.guiHuaTextViewText:@""};
            [gh addObject:dic1];
        }
        [dic setValue:gh forKey:@"gh"];
    }else{
        [dic setValue:[NSArray new] forKey:@"gh"];
    }


    if (_baoJianShouXuCell.huanbaoImageArr.count!=0) {
        //添加环保参数
        NSMutableArray *hb = [NSMutableArray array];
        for (NSString *imageString in _baoJianShouXuCell.huanbaoImageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"hb",@"attachdesc":_baoJianShouXuCell.huanBaoTextViewText.length!=0?_baoJianShouXuCell.huanBaoTextViewText:@""};
            [hb addObject:dic1];
        }
        [dic setValue:hb forKey:@"hb"];
    }else{
        [dic setValue:[NSArray new] forKey:@"hb"];
    }

    if (_baoJianShouXuCell.zhujianImageArr.count!=0) {
        //添加住建参数
        NSMutableArray *zj = [NSMutableArray array];
        for (NSString *imageString in _baoJianShouXuCell.zhujianImageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"zj",@"attachdesc":_baoJianShouXuCell.zhuJianTextViewText.length!=0?_baoJianShouXuCell.zhuJianTextViewText:@""};
            [zj addObject:dic1];
        }
        [dic setValue:zj forKey:@"zj"];
    }else{
        [dic setValue:[NSArray new] forKey:@"zj"];
    }
    
    if (_otherFilesCell.imageArr.count != 0) {
        //添加其他参数
        NSMutableArray *qt = [NSMutableArray array];
        for (NSString *imageString in _otherFilesCell.imageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"qt",@"attachdesc":_otherFilesCell.textViewText.length==0?@"":_otherFilesCell.textViewText};
            [qt addObject:dic1];
        }
        [dic setValue:qt forKey:@"qt"];
    }else{
        [dic setValue:[NSArray new] forKey:@"qt"];
    }
    
    if (_notesCell.imageArr.count != 0) {
        //添加备注参数
        NSMutableArray *qt = [NSMutableArray array];
        for (NSString *imageString in _notesCell.imageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"bz",@"attachdesc":_notesCell.textViewText.length==0?@"":_notesCell.textViewText};
            [qt addObject:dic1];
        }
        [dic setValue:qt forKey:@"bz"];
    }else{
        [dic setValue:[NSArray new] forKey:@"bz"];
    }
    
    [self hudWithText:@"信息上传中..."];
    [RequestManager postRequestWithModule:@"v1/savepm" param:dic model:nil success:^(NSDictionary *obj) {
        [self removeHud];
        NSDictionary *result = obj[@"result"];
        if ([result[@"check_result"] boolValue]) {
            [[UIApplication sharedApplication].keyWindow makeToast:@"新增项目成功!"];
            [self.navigationController popViewControllerAnimated:YES];
//            [(AppDelegate *)[UIApplication sharedApplication].delegate doLogin];
        }else{
            [self.view makeToast:result[@"message"]];
        }
    } failure:^(NSString *error) {
        [self.view makeToast:error];
        [self removeHud];
    }];
}
//更新项目
- (void)updateProjectAction{
    NSLog(@"更新项目");
    NSDictionary *dic = [self returnUpdatePmModel];
    if (dic==nil) {
        return;
    }
    [self hudWithText:@"信息保存中..."];
    [RequestManager postRequestWithModule:@"v1/updatepm" param:dic model:nil success:^(NSDictionary *obj) {
       
        if ([obj[@"result"][@"check_result"] boolValue]) {
            [self updateProjectAttach];
        }else{
            [self removeHud];
            [self.view makeToast:@"更新信息失败!"];
        }
    } failure:^(NSString *error) {
        [self removeHud];
        [self.view makeToast:error];
    }];
}

- (void)updateProjectAttach{
    NSDictionary *dic = [self returnAttachModel];
    [RequestManager postRequestWithModule:@"v1/currsaveattach" param:dic model:nil success:^(NSDictionary *obj) {
        [self removeHud];
        if ([obj[@"result"][@"check_result"] boolValue]) {
            [[UIApplication sharedApplication].keyWindow makeToast:@"更新信息成功!"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self removeHud];
            [self.view makeToast:@"更新信息失败!"];
        }
    } failure:^(NSString *error) {
        [self removeHud];
        [self.view makeToast:error];
    }];
}

/*
 * type 0:新增 1:更新
 */
- (NSDictionary *)returnUpdatePmModel{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    NSMutableDictionary *pmModel = [NSMutableDictionary dictionary];
    [dic setValue:User_Account forKey:@"account"];
    [dic setValue:User_SignToken forKey:@"token"];
    [pmModel setValue:_projectDetailModel.projectID forKey:@"id"];
    NSArray *keyArr = @[@"projectname",@"pmtype",@"pmxz",@"pmgm",@"pmstatus",@"pmposition",@"pmjsdw",@"pmdjdw",@"pmsgdw",@"pmjldw",@"pmsjdw",@"pmkcdw",@"pmjslx",@"pmjhztz",@"pmroadgrade",@"pmpgrzqk",@"pmstartday",@"pmdeliverday"];
 
    if ([_uploadTextStringArr[0] length]==0) {
        [self.view makeToast:@"请填写项目名称!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[0] forKey:keyArr[0]];
    
    if ([_uploadTextStringArr[1] length] ==0){
        [self.view makeToast:@"请选择项目类型!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[1] forKey:keyArr[1]];
    
    if ([_uploadTextStringArr[2] length] ==0){
        [self.view makeToast:@"请选择项目性质!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[2] forKey:keyArr[2]];
    
    if ([_uploadTextStringArr[3] length] ==0){
        [self.view makeToast:@"请选择项目规模!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[3] forKey:keyArr[3]];
    
    if ([_uploadTextStringArr[4] length] ==0){
        [self.view makeToast:@"请选择项目状态!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[4] forKey:keyArr[4]];
    
    if ([_uploadTextStringArr[5] length] ==0){
        [self.view makeToast:@"请填写项目位置!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[5] forKey:keyArr[5]];
    
    if ([_uploadTextStringArr[6] length] ==0){
        [self.view makeToast:@"请填写项目位置!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[6] forKey:keyArr[6]];
    
    if ([_uploadTextStringArr[7] length] ==0){
        [self.view makeToast:@"请填写建设单位!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[7] forKey:keyArr[7]];
    
    if ([_uploadTextStringArr[8] length] ==0){
        [self.view makeToast:@"请填写代建单位!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[8] forKey:keyArr[8]];
    
    if ([_uploadTextStringArr[9] length] ==0){
        [self.view makeToast:@"请填写施工单位!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[9] forKey:keyArr[9]];
    
    if ([_uploadTextStringArr[10] length] ==0){
        [self.view makeToast:@"请填写监理单位!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[10] forKey:keyArr[10]];
    
    if ([_uploadTextStringArr[11] length] ==0){
        [self.view makeToast:@"请填写设计单位!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[11] forKey:keyArr[11]];
    
    if ([_uploadTextStringArr[12] length] ==0){
        [self.view makeToast:@"请填写勘察单位!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[12] forKey:keyArr[12]];
    
    if ([_uploadTextStringArr[13] length] ==0){
        [self.view makeToast:@"请填写计划总投资!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[13] forKey:keyArr[13]];
    
    if ([_uploadTextStringArr[14] length] == 0 && [_uploadTextStringArr[1] integerValue] == 1){
        [self.view makeToast:@"请填写道路级别!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[14] forKey:keyArr[14]];
    
    if ([_uploadTextStringArr[5] length] ==0 && [_uploadTextStringArr[1] integerValue] == 2){
        [self.view makeToast:@"请填写融资情况!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[15] forKey:keyArr[15]];
    
    if ([_uploadTextStringArr[16] length] ==0){
        [self.view makeToast:@"请选择项目开工时间!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[16] forKey:keyArr[16]];
    
    if ([_uploadTextStringArr[17] length] ==0){
        [self.view makeToast:@"请项目交付时间!"];
        return nil;
    }
    [pmModel setValue:_uploadTextStringArr[17] forKey:keyArr[17]];
    
    if ([_uploadTextStringArr[16] integerValue] > [_uploadTextStringArr[17] integerValue]) {
        [self.view makeToast:@"项目交付时间不得早于项目开工时间!"];
        return nil;
    }
    
    [pmModel setValue:_otherFilesCell.textViewText.length==0?@"":_otherFilesCell.textViewText forKey:@"qtdesc"];
    [pmModel setValue:_technologicalPlanCell.textViewText.length==0?@"":_technologicalPlanCell.textViewText forKey:@"jjzbdesc"];
    [pmModel setValue:_baoJianShouXuCell.fagaiTextViewText.length==0?@"":_baoJianShouXuCell.fagaiTextViewText forKey:@"fgdesc"];
    [pmModel setValue:_baoJianShouXuCell.guoTuTextViewText.length==0?@"":_baoJianShouXuCell.guoTuTextViewText forKey:@"gtdesc"];
    [pmModel setValue:_baoJianShouXuCell.guiHuaTextViewText.length==0?@"":_baoJianShouXuCell.guiHuaTextViewText forKey:@"ghdesc"];
    [pmModel setValue:_baoJianShouXuCell.huanBaoTextViewText.length==0?@"":_baoJianShouXuCell.huanBaoTextViewText forKey:@"hbdesc"];
    [pmModel setValue:_baoJianShouXuCell.zhuJianTextViewText.length==0?@"":_baoJianShouXuCell.zhuJianTextViewText forKey:@"zjdesc"];
    [pmModel setValue:_notesCell.textViewText.length==0?@"":_notesCell.textViewText forKey:@"pmdesc"];
    [dic setValue:pmModel forKey:@"pmmodel"];
    return dic;
}

- (NSDictionary *)returnAttachModel{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:User_Account forKey:@"account"];
    [dic setValue:User_SignToken forKey:@"token"];
    [dic setValue:_projectDetailModel.projectID forKey:@"projectid"];
    [dic setValue:@"" forKey:@"dyid"];
    //添加图片参数
    NSMutableArray *picArr = [NSMutableArray array];
    for (NSString *imageString in _technologicalPlanCell.imageArr) {
        NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"zb",@"attachdesc":_technologicalPlanCell.textViewText.length==0?@"":_technologicalPlanCell.textViewText};
        [picArr addObject:dic1];
    }
    
    for (NSString *imageString in _baoJianShouXuCell.fagaiImageArr) {
        NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"fg",@"attachdesc":_baoJianShouXuCell.fagaiTextViewText.length!=0?_baoJianShouXuCell.fagaiTextViewText:@""};
        [picArr addObject:dic1];
    }
    for (NSString *imageString in _baoJianShouXuCell.guotuImageArr) {
        NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"gt",@"attachdesc":_baoJianShouXuCell.guoTuTextViewText.length!=0?_baoJianShouXuCell.guoTuTextViewText:@""};
        [picArr addObject:dic1];
    }
    for (NSString *imageString in _baoJianShouXuCell.guihuaImageArr) {
        NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"gh",@"attachdesc":_baoJianShouXuCell.guiHuaTextViewText.length!=0?_baoJianShouXuCell.guiHuaTextViewText:@""};
        [picArr addObject:dic1];
    }
    for (NSString *imageString in _baoJianShouXuCell.huanbaoImageArr) {
        NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"hb",@"attachdesc":_baoJianShouXuCell.huanBaoTextViewText.length!=0?_baoJianShouXuCell.huanBaoTextViewText:@""};
        [picArr addObject:dic1];
    }

    for (NSString *imageString in _baoJianShouXuCell.zhujianImageArr) {
        NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"zj",@"attachdesc":_baoJianShouXuCell.zhuJianTextViewText.length!=0?_baoJianShouXuCell.zhuJianTextViewText:@""};
        [picArr addObject:dic1];
    }
    if (_otherFilesCell.imageArr.count != 0) {
        //添加环保参数
        for (NSString *imageString in _otherFilesCell.imageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"qt",@"attachdesc":_otherFilesCell.textViewText.length==0?@"":_otherFilesCell.textViewText};
            [picArr addObject:dic1];
        }
    }
    if (_notesCell.imageArr.count != 0) {
        //添加环保参数
        for (NSString *imageString in _notesCell.imageArr) {
            NSDictionary *dic1 = @{@"attachurl":imageString,@"attachtype":@"bz",@"attachdesc":_notesCell.textViewText.length==0?@"":_notesCell.textViewText};
            [picArr addObject:dic1];
        }
    }
    
    [dic setObject:picArr forKey:@"attachlist"];
    return dic;
}

@end
